package com.rainng.coursesystem.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rainng.coursesystem.dao.mapper.CheckFileByPrjMapper;
import com.rainng.coursesystem.dao.mapper.CheckFileMapper;
import com.rainng.coursesystem.dao.mapper.TireInfoMapper;
import com.rainng.coursesystem.model.entity.TireInfoEntity;
import com.rainng.coursesystem.model.shitong.*;
import com.rainng.coursesystem.model.vo.response.ResultVO;
import com.rainng.coursesystem.module.task.entity.Task;
import com.rainng.coursesystem.module.task.entity.TaskChange;
import com.rainng.coursesystem.module.task.entity.TaskChangeVo;
import com.sun.xml.bind.v2.runtime.unmarshaller.XsiNilLoader;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

import static com.rainng.coursesystem.model.constant.GeneralReportProjectMap.ONLINE_PRJ_LIST;

@Service
public class TireInfoService extends BaseService {

    @Resource
    private TireInfoMapper mapper;

    public ResultVO countTireInfo(TireInfoEntity tie){
        if (StrUtil.isEmpty(tie.getGuige())) {
            return result(0);
        }
        int i = mapper.countTireInfo(tie);
        return result(i);
    }
    public List<Object> selectTireInfo(TireInfoEntity tie){
        if(StrUtil.isEmpty(tie.getGuige())){
            return new ArrayList<>();
        }
        Page<Object> pageParam = new Page<>(tie.getPage(),tie.getPageSize());
        IPage res = mapper.queryTireInfo(tie,pageParam);
        if(res == null){
            return new ArrayList<>();
        }
        return res.getRecords();
    }

    public ResultVO getTireGuige(){
        List<String> arr =  mapper.getTireGuiGe();
        return result(arr);
    }

    public ResultVO selectTireWidth(String info){
        String [] arr = info.trim().split(" ");
        List<Map<String,String>> res = new ArrayList<>();
        if(arr.length <= 1){
            Map result = new HashMap<>();
            result.put("duanmiankuandu",null);
            result.put("jingfuhebanjing",null);
            List<Map<String,String>> Object = new ArrayList<>();
            Object.add(result);
            return result(Object);
        }else {
            res = mapper.getTireWidth(arr[0],arr[1].replaceAll("[a-zA-Z]", ""));
            return result(res);
        }

    }

    public ResultVO editTaskChange(TaskChangeVo task){
        int i = mapper.saveTaskChange(task);
        int res = mapper.updateIsTaskChange(task.getFapplyNum());
        return result("添加成功");
    }

    public ResultVO selectTaskChange(String fApplyNum){
        List<Map<String,String>> task = mapper.queryTaskChange(fApplyNum);
        return result(task);
    }

    public ResultVO updateTaskChange(TaskChangeVo task){
        if(task.getNumId() == null&&(task.getTaskCode()!=null||task.getFapplyNum()!=null)){
            String numId = mapper.getNumId(task.getTaskCode(), task.getFapplyNum());
            task.setNumId(numId);
            int i = mapper.updateTaskChange(task);
            int k = mapper.updateSonNumId(task.getNumId(), task.getFapplyNum());
            return result("编辑成功");
        }else{
            int i = mapper.updateTaskChange(task);
            int k = mapper.updateSonNumId(task.getNumId(), task.getFapplyNum());
            return result("编辑成功");
        }
    }

    public ResultVO addTaskChangeInfo(String taskType){
        List<Map<String,String>> list = new ArrayList<>();
        list = mapper.getTaskChangeInfo(taskType);
        for(int i =0;i< list.size();i++){
            Map<String,String> info = list.get(i);
            TaskChangeVo task = new TaskChangeVo();
            task.setBatchId(info.get("batch_id"));
            task.setTaskType(info.get("task_type"));
            task.setTaskName(info.get("task_name"));
            task.setVeType(info.get("ve_type"));
            task.setVeModel(info.get("ve_model"));
            task.setUserId(info.get("user_id"));
            task.setCustomId(info.get("custom_id"));
            task.setFapplyNum(info.get("apply_num"));
            mapper.saveTaskChangeInfo(task);
        }
        return result("添加成功");
    }
}
